﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using CodeScales.Http;
using CodeScales.Http.Entity;
using CodeScales.Http.Entity.Mime;
using CodeScales.Http.Methods;
using Zone;

namespace Zone
{
    class SendCloud : BaseForm
    {

        private string _Api_User;
        public string Api_User
        {
            get { return _Api_User; }
            set { _Api_User = value; }
        }

        
        private string _Api_key;
        public string Api_key
        {
            get { return _Api_key; }
            set { _Api_key = value; }
        }

        private string _From;
        public string From
        {
            get { return _From; }
            set { _From = value; }
        }

        private string _FromName;
        public string FromName
        {
            get { return _FromName; }
            set { _FromName = value; }
        }
        private string _Label;
        public string Label
        {
            get { return _Label; }
            set { _Label = value; }
        }

   



        /// <summary>
        /// 使用sendcloud的WebAPI
        ///  </summary>
        public string WebAPISendMail(string revier,string subject,string content,string emlist)
        {
            string use_maillist = "false";
            CodeScales.Http.HttpClient client = new CodeScales.Http.HttpClient();
            HttpPost postMethod = new HttpPost(new Uri("http://sendcloud.sohu.com/webapi/mail.send.xml"));

            MultipartEntity multipartEntity = new MultipartEntity();
            postMethod.Entity = multipartEntity;
            if (!string.IsNullOrEmpty(emlist))
            { 
                revier = emlist;
                use_maillist = "true";
            }
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "api_user", Api_User));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "api_key", Api_key));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "from", From));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "fromname", FromName));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "to", revier));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "subject", subject));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "html", content));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "use_maillist", use_maillist));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "label", "554"));

            //FileInfo fileInfo = new FileInfo(@"E:\下载\CodeScales.Http.zip");

            //UTF8FileBody fileBody = new UTF8FileBody("file1", "附件.pdf ", fileInfo);
            //multipartEntity.AddBody(fileBody);

            HttpResponse response = client.Execute(postMethod);
            return EntityUtils.ToString(response.Entity);
            //System.Web.HttpContext.Current.Response.Write(response.ResponseCode);
            //System.Web.HttpContext.Current.Response.Write(EntityUtils.ToString(response.Entity));
            //Console.WriteLine("Response Code: " + response.ResponseCode);
            //Console.WriteLine("Response Content: " + EntityUtils.ToString(response.Entity));
        }

        /// <summary>
        /// 使用邮件列表进行群发
        ///  </summary>
        public string  WebAPISendMail_template(string subject, string content, string emlist,string lb)
        {
            CodeScales.Http.HttpClient client = new CodeScales.Http.HttpClient();
            HttpPost postMethod = new HttpPost(new Uri("http://sendcloud.sohu.com/webapi/mail.send_template.xml"));

            MultipartEntity multipartEntity = new MultipartEntity();
            postMethod.Entity = multipartEntity;
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "api_user", Api_User));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "api_key", Api_key));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "template_invoke_name", content));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "subject", subject));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "from", From));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "fromname", FromName));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "use_maillist", "true"));
            multipartEntity.AddBody(new StringBody(Encoding.UTF8, "to", emlist));
            if (!string.IsNullOrEmpty(lb)) multipartEntity.AddBody(new StringBody(Encoding.UTF8, "label", lb));
            


            HttpResponse response = client.Execute(postMethod);
            return EntityUtils.ToString(response.Entity);
            //System.Web.HttpContext.Current.Response.Write(response.ResponseCode);
            //System.Web.HttpContext.Current.Response.Write(EntityUtils.ToString(response.Entity));
        }
    }
}